'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac0204
    Inherits Snac

    Public Sub New()
        MyBase.New(&H2, &H4)
    End Sub

    Private _MimeType As New TlvMimeType
    Public ReadOnly Property MimeType() As TlvMimeType
        Get
            Return _MimeType
        End Get
    End Property

    Private _Capabilities As New TlvCapabilities
    Public ReadOnly Property Capabilities() As TlvCapabilities
        Get
            Return _Capabilities
        End Get
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        Throw New NotImplementedException
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        If _MimeType.CalculateDataSize > 0 Then
            data.AddRange(_MimeType.Serialize)
        End If

        data.AddRange(_Capabilities.Serialize)

        Return data
    End Function

    Public Overrides Function CalculateDataSize() As Integer
        If _MimeType.CalculateDataSize = 0 Then
            Return _Capabilities.CalculateTotalSize
        Else
            Return _MimeType.CalculateTotalSize + _Capabilities.CalculateTotalSize
        End If
    End Function
End Class

Public Class TlvMimeType
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H1)
    End Sub

    Private _MimeTypeName As String
    Public Property MimeTypeName() As String
        Get
            Return _MimeTypeName
        End Get
        Set(ByVal value As String)
            _MimeTypeName = value
        End Set
    End Property

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(_MimeTypeName))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        _MimeTypeName = ByteConverter.ToString(data.GetRange(4, DataSize))
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        If String.IsNullOrEmpty(_MimeTypeName) Then
            Return 0
        Else
            Return _MimeTypeName.Length
        End If
    End Function
End Class

Public Class TlvCapabilities
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H5)
    End Sub

    Private _Capabilites As New List(Of Guid)
    Public ReadOnly Property Capabilites() As List(Of Guid)
        Get
            Return _Capabilites
        End Get
    End Property

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        For Each cap As Guid In _Capabilites
            data.AddRange(ByteConverter.GetBytes(cap))
        Next

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        Do While index + 16 <= data.Count
            _Capabilites.Add(ByteConverter.ToGuid(data.GetRange(index, 16)))

            index += 16
        Loop
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Return _Capabilites.Count * 16
    End Function
End Class

Public NotInheritable Class IcqClientCapabilities
    Private Sub New()

    End Sub

    Private Shared ReadOnly _IcqFlag As Guid = New Guid("09461349-4C7F-11D1-8222-444553540000")
    Private Shared ReadOnly _IcqRouteFinder As Guid = New Guid("09461344-4C7F-11D1-8222-444553540000")
    Private Shared ReadOnly _RtfMessages As Guid = New Guid("97B12751-243C-4334-AD22-D6ABF73F1492")

    Public Shared ReadOnly Property IcqFlag() As Guid
        Get
            Return _IcqFlag
        End Get
    End Property

    Public Shared ReadOnly Property IcqRouteFinder() As Guid
        Get
            Return _IcqRouteFinder
        End Get
    End Property

    Public Shared ReadOnly Property RtfMessages() As Guid
        Get
            Return _RtfMessages
        End Get
    End Property
End Class